﻿Namespace Behavior

    Public NotInheritable Class TextBoxBehavior

        Private Sub New()
        End Sub

        Public Shared Function GetCommand(ByVal element As DependencyObject) As ICommand
            If element Is Nothing Then
                Throw New ArgumentNullException("element")
            End If

            Return DirectCast(element.GetValue(CommandProperty), ICommand)
        End Function

        Public Shared Sub SetCommand(ByVal element As DependencyObject, ByVal value As ICommand)
            If element Is Nothing Then
                Throw New ArgumentNullException("element")
            End If

            element.SetValue(CommandProperty, value)
        End Sub

        Public Shared ReadOnly CommandProperty As  _
                               DependencyProperty = DependencyProperty.RegisterAttached("Command", _
                               GetType(ICommand), GetType(TextBoxBehavior), _
                               New UIPropertyMetadata(Nothing, AddressOf OnCommandPropertyChanged))

        Public Shared Sub OnCommandPropertyChanged(ByVal element As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)

            If Not TypeOf e.NewValue Is ICommand Then
                Exit Sub
            End If

            Dim textBox = TryCast(element, TextBox)
            If textBox Is Nothing Then
                Exit Sub
            End If
            'AddHandler textBox.TextChanged, AddressOf OnTextBoxTextChanged
            AddHandler textBox.KeyDown, AddressOf OnTextBoxKeyDown

        End Sub

        Private Shared Sub OnTextBoxKeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
            Dim textBox = DirectCast(e.OriginalSource, TextBox)
            Dim command = TextBoxBehavior.GetCommand(textBox)
            If e.Key = Key.Enter AndAlso command IsNot Nothing AndAlso command.CanExecute(TextBoxBehavior.GetCommandParameter(textBox)) Then
                command.Execute(TextBoxBehavior.GetCommandParameter(textBox))
            End If
        End Sub

        'Private Shared Sub OnTextBoxTextChanged(ByVal sender As Object, ByVal e As TextChangedEventArgs)
        '    Dim textBox = DirectCast(e.OriginalSource, TextBox)
        '    Dim command = TextBoxBehavior.GetCommand(textBox)

        '    If command IsNot Nothing AndAlso command.CanExecute(TextBoxBehavior.GetCommandParameter(textBox)) Then
        '        command.Execute(TextBoxBehavior.GetCommandParameter(textBox))
        '    End If
        'End Sub

        Public Shared Function GetCommandParameter(ByVal element As DependencyObject) As Object
            If element Is Nothing Then
                Throw New ArgumentNullException("element")
            End If

            Return element.GetValue(CommandParameterProperty)
        End Function

        Public Shared Sub SetCommandParameter(ByVal element As DependencyObject, ByVal value As Object)
            If element Is Nothing Then
                Throw New ArgumentNullException("element")
            End If

            element.SetValue(CommandParameterProperty, value)
        End Sub

        Public Shared ReadOnly CommandParameterProperty As  _
                               DependencyProperty = DependencyProperty.RegisterAttached("CommandParameter", _
                               GetType(Object), GetType(TextBoxBehavior), _
                               New UIPropertyMetadata(Nothing))


    End Class
End Namespace
